package com.taotao.admin.rest;

import com.rop.client.CompositeResponse;
import com.taotao.admin.MyRopClient;
import com.taotao.admin.pojo.TableUI;
import com.taotao.rop.database.Constant;
import com.taotao.rop.database.domain.BaseDomain;
import com.taotao.rop.database.domain.City;
import com.taotao.rop.response.BaseResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.List;

@Controller
@RequestMapping(value = "/city")
public class CityController {
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Object getCityList(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");

        CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                .post(new BaseDomain(), BaseResponse.class, "city.list", "1.0");
        if (response.isSuccessful()){
            BaseResponse resp = response.getSuccessResponse();
            if (resp.getSuccess()){
                TableUI tableUI = new TableUI();
                List<LinkedHashMap<String, Object>> respData = (List<LinkedHashMap<String, Object>>) resp.getData();
                tableUI.setTotal(resp.getMsg());
                tableUI.setRows(respData);
                return tableUI;
            } else {
                return resp;
            }
        } else {
            return response.getErrorResponse().getSubErrors().get(0);
        }
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        City city = null;
        try {
            city = getCityFromRequest(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (city != null) {
            CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                    .post(city, BaseResponse.class, "city.insert", "1.0");
            if (response.isSuccessful()) {
                return response.getSuccessResponse();
            } else {
                return response.getErrorResponse().getSubErrors().get(0);
            }
        } else {
            BaseResponse resp = new BaseResponse();
            resp.setMsg("输入有误，请核对");
            return resp;
        }
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(HttpServletRequest request, @PathVariable String id) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        City city = null;
        try {
            city = getCityFromRequest(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (city != null) {
            city.setId(Long.parseLong(id));
            CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                    .post(city, BaseResponse.class, "city.update", "1.0");
            if (response.isSuccessful()) {
                return response.getSuccessResponse();
            } else {
                return response.getErrorResponse().getSubErrors().get(0);
            }
        } else {
            BaseResponse resp = new BaseResponse();
            resp.setMsg("输入有误，请核对");
            return resp;
        }
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object delete(HttpServletRequest request, @PathVariable String id) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        City city = new City();
        city.setId(Long.parseLong(id));
        city.setStatus(Constant.STATUS_DELETE);
        CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                .post(city, BaseResponse.class, "city.update", "1.0");
        if (response.isSuccessful()){
            return response.getSuccessResponse();
        } else {
            return response.getErrorResponse();
        }
    }

    @RequestMapping(value = "/find/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object findCityById(HttpServletRequest request, @PathVariable String id) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");

        City city = new City();
        city.setId(Long.parseLong(id));
        CompositeResponse<BaseResponse> response = MyRopClient.getRopClient(request.getHeader("sessionId")).buildClientRequest()
                .post(city, BaseResponse.class, "city.list", "1.0");
        if (response.isSuccessful()){
            BaseResponse resp = response.getSuccessResponse();
            if (resp.getSuccess()){
                return resp.getData();
            } else {
                return resp;
            }
        } else {
            return response.getErrorResponse().getSubErrors().get(0);
        }
    }

    private City getCityFromRequest(HttpServletRequest request) {
        String name = request.getParameter("name");
        String status = request.getParameter("status");

        City city = new City();
        city.setName(name);
        city.setStatus(Integer.parseInt(status));
        return city;
    }
}
